/**
 * Copyright (c) 2019, NewStrength. All rights reserved.
 * 2019-01-08 15:37:36
 */
package cn.newstrength.shangyujie.step;

import cn.newstrength.shangyujie.data.SYJUserObj;
import cn.newstrength.wtdf.plugin.service.JWTService;
import cn.newstrength.wtdf.plugin.service.RedisService;
import cn.newstrength.wtdf.plugin.util.TranUtils;
import cn.newstrength.wtdf.web.databus.DataContext;
import cn.newstrength.wtdf.web.exception.PAIException;
import cn.newstrength.wtdf.web.processor.Operation;
import cn.newstrength.wtdf.web.processor.OperationStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class SYJCreateJWTTokenStep implements OperationStep {

	private final static Logger logger = LoggerFactory.getLogger(SYJCreateJWTTokenStep.class);

	@Override
	public int excute(Operation oper) throws PAIException {
		/*
		 * 用户登录成功后，返回用户数据
		 */
		SYJUserObj userObj = new SYJUserObj();
		userObj.setId(666);
		userObj.setCode("Xd");
		userObj.setPassword("123456");
		logger.info("用户登录成功 {}",TranUtils.toJson(userObj));
		/*
		 * 生成Token
		 */
		String jwtToken = JWTService.createJWT(userObj.getCode());
		logger.info("用户Token生成 {}",jwtToken);
		/*
		 * 将token作为key，userObj作为value存储到Redis缓存中
		 */
		RedisService redis = oper.getBean("redisService", RedisService.class);
		redis.put("cn:newstrength:nsms:oauth2:token:".concat(jwtToken), userObj,30);
		logger.info("用户Token缓存生成！");
		/*
		 * 回传数据
		 */
		DataContext dc = oper.getContext();
		logger.info("Token info：{}",JWTService.toMap(jwtToken));

		dc.setTranStepResult(0,"token格式化",jwtToken);
		return 0;
	}

	@Override
	public int init(Map<String, String> input) {
		return 0;
	}

}
